package com.zbxx.dao.impl;

import com.zbxx.dao.EmployeeDao;
import com.zbxx.domain.Employee;
import org.hibernate.criterion.DetachedCriteria;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.util.List;

/*
* 员工管理dao的实现类
* */
public class EmployeeDaoImpl extends HibernateDaoSupport implements EmployeeDao {
        //Dao中根据用户名和密码查询用户的方法
    @Override
    public Employee findByUsernameAndPassword(Employee employee) {
        String hql="from Employee where username=? and password=?";
        List<Employee> list= this.getHibernateTemplate().find(hql,employee.getUsername(),employee.getPassword());
        if(list.size()>0){
            return list.get(0);
        }
        return null;
    }

    //统计个数
    @Override
    public int findCount() {
        String hql="select count(*) from Employee";
        List<Long> list= this.getHibernateTemplate().find(hql);
        if(list.size()>0){
            return list.get(0).intValue();
        }
        return 0;
    }

    //分页查询
    @Override
    public List<Employee> findByPage(int begin, int pageSize) {
        DetachedCriteria criteria=DetachedCriteria.forClass(Employee.class);
        List<Employee> list= this.getHibernateTemplate().findByCriteria(criteria,begin,pageSize);
        return list;
    }

    //dao中保存员工的方法
    @Override
    public void save(Employee employee) {
        this.getHibernateTemplate().save(employee);
    }

    @Override
    public Employee finById(Integer eid) {
        return this.getHibernateTemplate().get(Employee.class,eid);
    }

    @Override
    public void update(Employee employee) {
        System.out.println(employee.getDepartment().getDid());
        this.getHibernateTemplate().update(employee);
    }

    @Override
    public void delete(Employee employee) {
            this.getHibernateTemplate().delete(employee);
    }
}
